package com.borball.store.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
//
//import org.pushingpixels.substance.api.SubstanceLookAndFeel;
//import org.pushingpixels.substance.api.skin.CremeCoffeeSkin;

import com.borball.store.util.UserUtils;

public class LoginFrame extends JFrame {

	private static final long serialVersionUID = 4884141395874765728L;
	private JTextField textFieldUserName;
	private JPasswordField pwdFieldPassword;
	private JButton buttonLogin;
	private JButton btnCancel;
	private JLabel lblErrorMessage;

	public LoginFrame() {
		setTitle("\u767B\u9646");
		getContentPane().setBackground(SystemColor.activeCaption);
		getContentPane().setLayout(null);
		getContentPane().setPreferredSize(new Dimension(334, 218));

		JLabel lblUserName = new JLabel("\u7528\u6237\u540D\uFF1A");
		lblUserName.setFont(new Font("��Բ", Font.PLAIN, 15));
		lblUserName.setBounds(64, 49, 73, 25);
		getContentPane().add(lblUserName);

		JLabel lblPassword = new JLabel("\u5BC6  \u7801\uFF1A");
		lblPassword.setFont(new Font("��Բ", Font.PLAIN, 15));
		lblPassword.setBounds(64, 86, 73, 25);
		getContentPane().add(lblPassword);

		textFieldUserName = new JTextField();
		textFieldUserName.setFont(new Font("��Բ", Font.PLAIN, 14));
		textFieldUserName.setBounds(147, 49, 128, 25);
		getContentPane().add(textFieldUserName);
		textFieldUserName.setColumns(10);
		textFieldUserName.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				pwdFieldPassword.setText("");
				lblErrorMessage.setText("");
			}
		});

		pwdFieldPassword = new JPasswordField();
		pwdFieldPassword.setBounds(147, 86, 128, 25);
		getContentPane().add(pwdFieldPassword);
		pwdFieldPassword.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				lblErrorMessage.setText("");
			}
		});
		
		
		ActionHandler actionHandler = new ActionHandler();
		
		buttonLogin = new JButton("\u767B\u9646");
		buttonLogin.addActionListener(actionHandler);
		buttonLogin.setBackground(SystemColor.window);
		buttonLogin.setFont(new Font("��Բ", Font.PLAIN, 16));
		buttonLogin.setBounds(64, 156, 93, 25);
		getContentPane().add(buttonLogin);

		btnCancel = new JButton("\u53D6\u6D88");
		btnCancel.setBackground(SystemColor.window);
		btnCancel.setFont(new Font("��Բ", Font.PLAIN, 16));
		btnCancel.addActionListener(actionHandler);

		btnCancel.setBounds(178, 156, 99, 25);
		getContentPane().add(btnCancel);

		getRootPane().setDefaultButton(buttonLogin);

		lblErrorMessage = new JLabel("");
		lblErrorMessage.setFont(new Font("��Բ", Font.PLAIN, 12));
		lblErrorMessage.setBounds(64, 121, 211, 25);
		getContentPane().add(lblErrorMessage);
		setResizable(false);

		pack();
		setLocationRelativeTo(null);
	}

	private class ActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();

			if (source == buttonLogin) {
				login(textFieldUserName.getText(), String.valueOf(pwdFieldPassword.getPassword()));
			} else if (source == textFieldUserName) {

			} else if (source == btnCancel) {
				System.exit(0);
			}
		}
	}

	public void login(String name, String password) {
		int i = UserUtils.getInstance().isValidUser(name, password);
		i=0;
		if (i == UserUtils.USER_NOT_EXIST)
			lblErrorMessage.setText("���û�������!");
		else if (i == UserUtils.WRONG_PASSWORD)
			lblErrorMessage.setText("�������!");
		else {
			MainFrame frame = new MainFrame();
			frame.setVisible(true);
			this.dispose();
		}
	}

	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
//				SubstanceLookAndFeel.setSkin(new BusinessBlueSteelSkin());
//				SubstanceLookAndFeel.setSkin(new AutumnSkin());
//				SubstanceLookAndFeel.setSkin(new BusinessSkin());
//				SubstanceLookAndFeel.setSkin(new ChallengerDeepSkin());
//				SubstanceLookAndFeel.setSkin(new CremeCoffeeSkin());
				
//				SubstanceLookAndFeel.setSkin(new CremeSkin());
				
				
//				SubstanceLookAndFeel.setSkin(new OfficeBlue2007Skin());
				
//				SubstanceLookAndFeel.setSkin(new SaharaSkin());
				
				LoginFrame frame = new LoginFrame();
				frame.setVisible(true);
			}
		});

	}
}
